package ComparableTest;//测试代码

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

public class ProductSort {

    public static void main(String[] args) throws ParseException {
        Product pro1 = new Product();
        pro1.setName("pro1");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-MM",Locale.CHINA);
        pro1.setDate(sdf.parse("2009-03-23"));
        pro1.setPrice(89);
        Product pro2 = new Product();
        pro2.setName("proc2");
        pro2.setDate(sdf.parse("2009-02-23"));
        pro2.setPrice(45);
        Product pro3 = new Product();
        pro3.setName("proc3");
        pro3.setDate(sdf.parse("2009-01-23"));
        pro3.setPrice(83);
        Product pro4 = new Product();
        pro4.setName("proc4");
        pro4.setDate(sdf.parse("2009-01-23"));
        pro4.setPrice(800);


        List<Product> productList = new ArrayList<Product>();
        productList.add(pro1);
        productList.add(pro3);
        productList.add(pro2);
        productList.add(pro4);

        System.out.println("排序前————————————————————");
        for(Product p: productList){
            System.out.println(p.getName()+"----"+p.getPrice()+"----"+p.getDate());
        }
        ProductComparable sort = new ProductComparable();

        ProductComparable.sortASC = false;//降序   
        ProductComparable.sortByPrice=true;  //设置排序属性生效 
        Collections.sort(productList,sort);

        System.out.println("排序后————————————————————");

        for(Iterator<Product>  iter = productList.iterator(); iter.hasNext();){
            Product p = (Product)iter.next();
            System.out.println(p.getName()+"----"+p.getPrice()+"---"+p.getDate());
        }
    }
}  